<?php

namespace app\common\model;

use think\Env;
use think\Model;

/**
 * 模型
 */
class CarBrands extends Model
{
    // 表名
    protected $name = 'car_brands';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function setLogoAttr($value)
    {
        if (!empty($value)){
            $arr = parse_url($value);
            return $arr['path'];
        }
        return '';
    }


    public function getLogoAttr($value) {
        return $value ? Env::get('app.app_host').$value : '';
    }


    /**
     * 获取所有品牌
     * @return array
     * @throws \think\exception\DbException
     */
    public static function geAllBrands()
    {
        $brands = self::field('id brand_id,name,letter,logo,is_hot')->where('is_del',0)->select()->toArray();
        //把获取到首字母的数组排序
        $sort="SORT_ASC";
        $brands= arraySequence($brands, "letter",$sort);
        //创建新数组
        $data = [];
        //通过字段相同的分到一个新数组
        foreach($brands as &$v){
            $data[$v['letter']][]=$v;
        }
        return $data;
    }


    /**
     * 获取热门品牌
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getHotBrand()
    {
        $list = self::field('id brand_id,name,letter,logo,is_hot')->where('is_hot',1)->limit(10)->order('id asc')->select()->toArray();
        return $list;
    }








}
